<template>
<div class="bg-img">
  <div class="bg" :style="{display:isShowLogin}">
            <div class="register">
                
                <div class="register-header">
                    
                    <div class="QC-code" @click="backPage">
                        <i class="fa fa-mail-reply fa-2x"></i>
                    </div>
                    
                    </div>
                    <div class="identity">
                        <div @click="identityChange">老师注册<div class="checked active" ></div></div>
                        
                        <div @click="identityChange">学生注册<div class="checked" ></div></div>
                    </div>
                    <div class="register-info">
                        
                        <div class="register-css">
                            <div><i class="fa fa-user fa-2x"></i></div><input placeholder="请输入用户名" type="text" name="username" v-model="username" @input="handleInputChange"><br>
                        </div>
                        <div class="register-css">
                            <div><i class="fa fa-key fa-2x"></i></div><input placeholder="请输入密码" type="password" name="password" v-model="password" @input="handleInputChange"><br>
                        </div>
                        
                        <div class="register-css">
                            <div><i class="fa fa-key fa-2x"></i></div><input placeholder="再次输入密码" type="password" name="password" v-model="passwordAgain" @input="handleInputChange"><br>
                        </div>
                        
                        
                    </div>

                
                    <div class="btn" v-show="isShowZhuce" @click="sendNewData">
                        <button class="zhuce">注册</button>
                    </div>
            </div>
        </div>
        
        </div>
</template>

<script>
import axios from 'axios'

export default {
    name:'RegiterPage',
    data(){
        return {
            isShowLogin: 'block',
            identity: '老师登录',
            username: '',
            password: '',
            passwordAgain:'',
            dengluStyle:{
                color:'',
                backgroundColor:'',
                cursor:'',
                
            },
            isButtonDisabled: false,
            isShowZhuce: true
        }
    },
    methods:{
        handleInputChange(){
            
            if(this.username === '' | this.password === ''){
                this.dengluStyle.color = ''
                this.dengluStyle.backgroundColor = ''
                this.dengluStyle.cursor = ''
                this.isButtonDisabled = true
            }else{
                this.dengluStyle.color = 'rgb(206, 180, 137)'
                this.dengluStyle.backgroundColor = 'rgb(57, 59, 64)'
                this.dengluStyle.cursor = 'pointer'
                this.isButtonDisabled = false
            }
            

        },
        identityChange(e){
            const active = document.querySelector('.active')
            active.classList.remove('active')
            this.identity = e.target.textContent
            e.target.querySelector('.checked').classList.add('active')
            if(this.identity === '管理员登录'){
                this.isShowZhuce = false
            }else{
                this.isShowZhuce = true
            }
        },
        
        sendData(){
            // TODO
            axios.post('用户信息提交位置',{
                identity: this.identity,
                username:this.username,
                password:this.password

            }).then(response => {
                console.log("数据发送成功：",response.data)
            }).catch(error => {
                console.log("数据发送失败：", error)
            })
        },
        backPage(){
            window.location.replace('/#/')
            
        },
        sendNewData(){
            // Todo
            if(this.password === this.passwordAgain & this.password!==''){
                if(this.identity === '老师登录'){
                    axios.post('/prod-api/teacher',{
                            
                            username:this.username,
                            password:this.password

                        }).then(response => {
                            if (response.data.code === 0) {
                                this.$message({
                                type: 'success',
                                message: '注册成功!'
                            })
                            this.$router.push({path:'/#/', query:{username:this.username}})
                            }
                            
                            else{
                                alert(response.data.msg)
                            }
                        })
                }else{
                    axios.post('/prod-api/student',{
                            
                            username:this.username,
                            password:this.password

                        }).then(response => {
                            if (response.data.code === 0) {
                                this.$message({
                                type: 'success',
                                message: '注册成功!'
                            })
                            this.$router.push({path:'/#/', query:{username:this.username}})
                            }
                            
                            else{
                                alert(response.data.msg)
                            }
                        })
                }
                    
            }else{
                        
                alert('两次密码不相同或者密码为空')
            }
                    
        },
        close(){
            this.isShowLogin = 'none'
        }

    },
    components: {
        
    },

}
</script>

<style>
.bg-img{
    
    width: 100%;
    height: 100%;
    background-image: url('./assets/icon_bg_logo2.png') ;
    
    background-size: cover; /* 设置背景图片尺寸为覆盖整个元素 */
    background-repeat: no-repeat; /* 防止背景图片重复 */
    background-position: center; /* 将背景图片放置在元素中心 */

}

.bg{
    display: block;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, .5);
    z-index: 9999;
}


.register{
    background-color: white;
    width: 500px;
    height: 400px;
    position: absolute;
    top: calc(50vh - 200px);;
    left: calc(50% - 250px);
    border-radius: 10px;
}
.register-css input{
    height: 20px;
    padding-left: 10px;
}
.register-header{
    width: 100%;
    height: 50px;
}
.QC-code,
.register-close{
    height: 100%;
    width: 50px;
}
.QC-code{
    float: left;
}
.register-close{
    float: right;
}
.register-header i{
    margin-top: 10px;
    
}
.register-header i:hover{
    color: yellowgreen;
    cursor: pointer;
}
.register-info .register-css div{
    float: left;
    width: 70px;
    height: 50px;
    font-size: 15px;
    margin: 8px 10px;
    border-right: 1px solid rgba(0, 0, 0, .5);
}
.register-info .register-css div i{
    margin-top: 8px;
    margin-left: 20px;
}
.register-css input{
    width: 300px;
    height: 40px;
    font-size: 15px;
    border-radius: 10px;
    margin-top: 8px;
}
.register-css{
    width: 450px;
    height: 60px;
    border: 1px solid rgb(228, 229, 235);
    margin: 15px 25px;
    border-radius: 15px;
}
.register-css:hover{
    border: 1px solid rgb(211, 188, 142);
    transition: all 0.3s ease;
}

.register .identity{
    height: 50px;
}
.register .identity div{
    float: left;
    height: 50px;
    width: 133px;
    margin: 0 55px;
    font-size: 15px;
    text-align: center;
    line-height: 50px;
    position: relative;
}
.register .identity div:hover{
    cursor: pointer;
}
.register .identity .checked{
    width: 35px;
    height: 5px;
    display: none;
    border-radius: 5px;
    background-color: rgb(211, 188, 142);
    position: absolute;
    bottom: 0;
    left: 0px;
}
.register .identity .active{
    display: block;
}


.register .btn{
    width: 400px;
    height: 40px;
    margin: 15px 50px;
}
.register .btn .denglu,
.register .btn .zhuce{
    width: 100%;
    height: 100%;
    
    border:none;
    border-radius: 10px;
    background-color: #fff;
}
.register .btn .zhuce:hover{
    background-color: rgb(57, 59, 64);
    color: rgb(206, 180, 137);
    cursor: pointer;
}
</style>